//
//  HousesView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/27.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "HousesView.h"
#import "Masonry.h"
#import "RefreshUtil.h"
#import "HousesViewModel.h"

@interface HousesView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) HousesViewModel *housesViewModel;
@end

@implementation HousesView
{
    NSInteger _pageIndex;
}
#pragma mark - Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadHousesView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - Delegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_models != nil) {
        return _models.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *housesCellIdentifier = @"HousesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:housesCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:housesCellIdentifier];
    }
    HousesModel *houseModel = _models[indexPath.row];
    NSString *houseTitle = houseModel.houseLocation;
    cell.textLabel.text = houseTitle;
    cell.textLabel.textColor = TextColor120;
    cell.textLabel.font = TextFont14;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    self.titleLabel.text = [NSString stringWithFormat:@"  %@下房源(%ld)",_houseType,_housesCount];
    return self.titleLabel;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return ScreenHeight*0.07;
}

#pragma mark - Delegate UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DDLogInfo(@"房源信息点击事件%ld",indexPath.row);
    if (_delegate != nil && [_delegate respondsToSelector:@selector(housesTableDidSelectWithHouseModel:)]) {
        [_delegate housesTableDidSelectWithHouseModel:_models[indexPath.row]];
    }
}

#pragma mark - Event Response
- (void)headerRefresh{
    _pageIndex = 0;
    [self loadNetModel];
}

- (void)footerRefresh{
    _pageIndex ++;
    [self loadNetModel];
}

#pragma mark - Private
- (void)loadHousesView{
   [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(self);
   }];
}

#pragma mark - Public
- (void)loadNetModel{
    NSDictionary *postDic = @{@"pageIndex":@(_pageIndex),@"pageSize":@"10"};
    self.housesViewModel.layoutId = _selectLayoutId;
    self.housesViewModel.postObject = postDic;
    [self.housesViewModel loadRequest];
}

#pragma mark - Getter
- (UITableView *)tableView{

    if (_tableView == nil) {
        _tableView = [UITableView new];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = ScreenHeight*0.07;
        _tableView.mj_header = [RefreshUtil loadRefreshGifHeaderWithTarget:self andRefreshingAction:@selector(headerRefresh)];
        _tableView.mj_footer = [RefreshUtil loadRefreshGifFooterWithTarget:self andRefreshingAction:@selector(footerRefresh)];
        [self addSubview:_tableView];
    }
    return _tableView;
}

- (UILabel *)titleLabel{

    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 40)];
        _titleLabel.textColor = TextColor80;
        _titleLabel.font = TextFont18;
        _titleLabel.backgroundColor = [UIColor whiteColor];
    }
    return _titleLabel;
}

- (HousesViewModel *)housesViewModel{
    if (_housesViewModel == nil) {
        _housesViewModel = [HousesViewModel new];
        _housesViewModel.viewObject = self;
    }
    return _housesViewModel;
}

#pragma mark - Setter
- (void)setModels:(NSMutableArray *)models{
    if (_pageIndex == 0) {
        _models = models;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }else if (_pageIndex != 0 && models.count == 0){
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }else{
        [self.tableView.mj_footer endRefreshing];
        [_models addObjectsFromArray:models];
    }
    [self.tableView reloadData];
}

- (void)setHousesCount:(NSInteger)housesCount{
    _housesCount = housesCount;
    [self.tableView reloadData];
}

- (void)setHouseType:(NSString *)houseType{
    _houseType = houseType;
    [self.tableView reloadData];
}

@end
